package com.zhiletu.collect.config;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zhiletu.collect.util.DBUtil;


/**
 *
 * @Title: RuleConfig.java
 * @Package com.zhiletu.collect.config
 * @Description: 采集规则配置
 * @author zhiletu.com
 * @date 2021年3月11日
 * @version V1.0
 */
public class RuleConfig {

	/**
	 * @desc 根据域名配置编号查询一个域名的全局配置
	 * @param configId
	 * @return
	 */
	public static Map<String, Object> queryCollectConfigById(String configId) {

		String sql = "select * from collect_config where config_id=?";

		List<Map<String, Object>> list = RuleConfig.querySql(sql, new Object[] { configId });

		return list.size() > 0 ? list.get(0) : new HashMap<String, Object>();
	}

	/**
	 * @desc 根据规则配置编号查询一个域名的全局配置
	 * @param ruleId
	 * @return
	 */
	public static Map<String, Object> queryCollectConfigByRuleId(String ruleId) {

		String sql = "select * from collect_config where config_id=(select config_id from collect_rule where rule_id=?)";

		List<Map<String, Object>> list = RuleConfig.querySql(sql, new Object[] { ruleId });

		return list.size() > 0 ? list.get(0) : new HashMap<String, Object>();
	}

	/**
	 * @desc 根据规则配置编号查询规则配置
	 * @param ruleId
	 * @return
	 */
	public static Map<String, Object> queryRuleConfigById(String ruleId) {

		String sql = "select * from collect_rule where rule_id=?";

		List<Map<String, Object>> list = RuleConfig.querySql(sql, new Object[] { ruleId });

		return list.size() > 0 ? list.get(0) : new HashMap<String, Object>();
	}

	/**
	 * @desc 查询采集服务器配置
	 * @param sql
	 * @param parmas
	 * @return
	 */
	public static List<Map<String, Object>> querySql(String sql, Object[] parmas) {
		String[] dbparam = dbparam();

		return DBUtil.querySql(sql, parmas, dbparam[0], dbparam[1], dbparam[2], dbparam[3], dbparam[4]);
	}

	/**
	 * @desc 查询采集服务器配置
	 * @param sql
	 * @return
	 */
	public static List<Map<String, Object>> querySql(String sql) {
		String[] dbparam = dbparam();

		return DBUtil.querySql(sql, dbparam[0], dbparam[1], dbparam[2], dbparam[3], dbparam[4]);
	}

	/**
	 * @desc 更新或新增一条记录到採集服務器配置
	 * @param sql
	 * @param parArray
	 * @return
	 */
	public static int update(String sql, Object[] parArray) {
		String[] dbparam = dbparam();

		return DBUtil.update(sql, parArray, dbparam[0], dbparam[1], dbparam[2], dbparam[3], dbparam[4]);
	}

	private static String[] dbparam() {
		String ip = CollectConfig.getIp();// 采集配置所在的数据库
		String port = CollectConfig.getPort();
		String dbname = CollectConfig.getDBname();
		String username = CollectConfig.getUsername();
		String passwd = CollectConfig.getPasswd();
		return new String[] { ip, port, dbname, username, passwd };
	}
}
